package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysDictDataVo;
import com.ruoyi.system.domain.bo.SysDictDataBo;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典数据
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dictData")
public class SysDictDataController extends BaseController {

    private final ISysDictDataService iSysDictDataService;

    /**
     * 查询字典数据列表
     */
    @SaCheckPermission("system:dictData:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictDataVo> list(SysDictDataBo bo, PageQuery pageQuery) {
        return iSysDictDataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出字典数据列表
     */
    @SaCheckPermission("system:dictData:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysDictDataBo bo, HttpServletResponse response) {
        List<SysDictDataVo> list = iSysDictDataService.queryList(bo);
        ExcelUtil.exportExcel(list, "字典数据", SysDictDataVo.class, response);
    }

    /**
     * 获取字典数据详细信息
     *
     * @param dictCode 主键
     */
    @SaCheckPermission("system:dictData:query")
    @GetMapping("/{dictCode}")
    public R<SysDictDataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long dictCode) {
        return R.ok(iSysDictDataService.queryById(dictCode));
    }

    /**
     * 新增字典数据
     */
    @SaCheckPermission("system:dictData:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysDictDataBo bo) {
        return toAjax(iSysDictDataService.insertByBo(bo));
    }

    /**
     * 修改字典数据
     */
    @SaCheckPermission("system:dictData:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysDictDataBo bo) {
        return toAjax(iSysDictDataService.updateByBo(bo));
    }

    /**
     * 删除字典数据
     *
     * @param dictCodes 主键串
     */
    @SaCheckPermission("system:dictData:remove")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] dictCodes) {
        return toAjax(iSysDictDataService.deleteWithValidByIds(Arrays.asList(dictCodes), true));
    }
}
